package com.xinsilu.product.service.impl;

import com.xinsilu.product.mapper.ProductMapper;
import com.xinsilu.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Transactional
    public  int saveOrder(int id,int buyCount){
        int productCount=productMapper.getProductCountByID(id);
        if(productCount>=buyCount){
            productMapper.updateProductCountById(id, buyCount);
            System.out.println("购买成功");
            return 0;
        }else {
            System.out.println("库存不够");
            return -1;
        }




    }

    @Override
    public String saveOrderRedis(String id, int buyCount) {
      String result=  redisTemplate.execute(new SessionCallback<String>() {
          @Override
          public  String execute(RedisOperations Operations) throws DataAccessException {
              Operations.watch(id);//监听商品的key
              String productCount = redisTemplate.opsForValue().get(id);//从redis中取出商品库存
              Operations.multi();//开启事务
              int prductCountInt = Integer.parseInt(productCount);
              if(prductCountInt>=buyCount){
                  prductCountInt=prductCountInt - buyCount;
                  redisTemplate.opsForValue().set(id,prductCountInt+"");
                  System.out.println("购买成功");
              }else {
                  System.out.println("库存不够");
              }
              List<String> resultList= Operations.exec();//执行事务队列中的命令，返回结果列表
              if(resultList == null || resultList.size() == 0){
                  return "fail";
              }else{
                  return "d";
              }
          }
      });
        return result;
    }
    //此方法找到lua脚本，准备脚本需要的key和arg
    @Override

    public Long saveOrderLua(String id, int buyCount) {
        DefaultRedisScript<Long> script = new DefaultRedisScript();
        script.setLocation(new ClassPathResource("/lua/saveOrder.lua")); //找到lua脚本文件的位置
        script.setResultType(Long.class);  //设置返回值类型
        List<String> keyList = new ArrayList<String>();//准备脚本需要的key列表
        keyList.add(id);
        Long result= redisTemplate.execute(script,keyList,buyCount+"");//执行脚本
        return result;
    }
}
